package com.yincheng.oes.business.util;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

/**
 * @Author: yangsy
 * @Contact: 446948434@qq.com
 * @Date: 2021/9/1 21:52
 * @Version: 1.0
 * @Description:
 */
public class TimeUtils {
    private static final SimpleDateFormat yyMMdd = new SimpleDateFormat("yyyyMMdd");

    private static final SimpleDateFormat yyMMddHHmm = new SimpleDateFormat("yyyyMMddHHmm");

    public static String getYYMMdd() {
        return getYYMMdd(new Date());
    }

    public static String getYYMMdd(Date date) {
        return yyMMdd.format(date);
    }

    public static String getYyMMddHHmm() {
        return yyMMddHHmm.format(new Date());
    }

    /**
     * 为Date增减天数（天数传负数为减）
     *
     * @param date 日期
     * @param days 天数
     * @return 增减后的日期
     */
    public static Date addAndReduceDays(Date date, Long days) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        LocalDateTime localDateTime = dateTime.plusDays(days);
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date localDate2Date(LocalDate localDate) {
        if (null == localDate) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }
}
